﻿// Copyright (c) Kona Framework Team. All rights reserved.
// For continued development and updates, visit http://www.codeplex.com/kona
// For a copy of the license please vist: http://www.codeplex.com/kona/license

using System.Collections.Generic;

namespace Kona.Data.Provider
{
    public class AdoMdConnectionPoolFactory : IAdoMdConnectionPoolFactory
    {
        static readonly AdoMdConnectionPoolFactory instance = new AdoMdConnectionPoolFactory();

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
// ReSharper disable EmptyConstructor
        static AdoMdConnectionPoolFactory()
// ReSharper restore EmptyConstructor
        {
        }

        AdoMdConnectionPoolFactory()
        {
        }

        public static AdoMdConnectionPoolFactory Instance
        {
            get
            {
                return instance;
            }
        }

        private readonly Dictionary<string, AdoMdConnectionPool> pools = new Dictionary<string, AdoMdConnectionPool>();
        readonly object lockObject = new object();

        public AdoMdConnectionPool GetConnectionPool(string connectionString)
        {
            
            if(this.pools.ContainsKey(connectionString))
            {
                return this.pools[connectionString];    
            }

            AdoMdConnectionPool FoundPool = new AdoMdConnectionPool(new AdomdClientConnectionFactory(connectionString), 5, connectionString);
            lock (lockObject)
                this.pools.Add(connectionString,FoundPool);

            return FoundPool;
        }
    }
}
